<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\admin\model\goods\Order;
use app\common\controller\Api;
use app\common\controller\Payment;
use think\Controller;
use think\Db;
use think\Request;


class Userfriends extends Api
{
    private static $model;
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new \app\common\model\UserFriends();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function lists()
    {
        $userId = $this->auth->id;
        $where = [
            'user_id' => $userId,
            'status' => 1
        ];

        $arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        $data = [];
        foreach ($arr as $val) {
            $lists = self::$model->where($where)->where('friend_first', $val)->order('createtime desc')->select();
            if ($lists) {
                $data[$val] = $lists;
            }
        }

        $data1 = self::$model->where($where)->where("friend_first", 0)->order('createtime desc')->select();
        $data['0'] = $data1;
        $data = array_filter($data);

        $this->success('我的好友列表', $data);
    }

    /**
     * 根据关键字查询好友
     */
    public function getListsByKeyword()
    {
        $keyword = input('keyword');
        if (!$keyword) {
            $this->error('请输入关键字！');
        }

        $pageSize = input('pagesize', 15);

        $userId = $this->auth->id;
        $where = [
            'user_id' => $userId,
            'status' => 1,
            'friend_nckname' => ['like', '%' . $keyword . '%']
        ];

        $lists = self::$model->where($where)->order('createtime desc')->paginate($pageSize);
        $total = self::$model->where($where)->count();

        $this->success('关键字查询好友列表', ['list' => $lists, 'total' => $total]);
    }

    /**
     * 根据关键词查询待加好友
     */
    public function getNewListsByKeyword()
    {
        $mobile = input('mobile');
        if (!$mobile) {
            $this->error('请输入手机号码！');
        }

        $pageSize = input('pagesize', 15);

        $userId = $this->auth->id;
        $map = [
            'user_id' => $userId,
            'status' => ['in', [0, 1]]
        ];

        $uids = self::$model->where($map)->column('friend_user_id');
        array_push($uids, $userId);
        $where = [
            'status' => 'normal',
            'mobile' => ['like', '%' . $mobile . '%'],
            'id' => ['not in', $uids]
        ];

        $lists = \app\common\model\User::field('id,nickname,avatar,gender')->where($where)->order('createtime desc')->paginate($pageSize);
        $total = \app\common\model\User::where($where)->count();

        $this->success('关键字查询待添加好友列表', ['list' => $lists, 'total' => $total]);
    }

    /**
     * 发送好友申请
     */
    public function create()
    {
        $content = input('content');
        if (!$content) {
            $this->error('请输入添加介绍！');
        }

        $friend_user_id = input('user_id');
        if (!$friend_user_id) {
            $this->error('参数缺失！');
        }

        $user_id = $this->auth->id;
        if ($user_id == $friend_user_id) {
            $this->error('不可添加自己！');
        }

        $friendInfo = \app\common\model\User::where('status', 'normal')->find($friend_user_id);
        if (!$friendInfo) {
            $this->error('该用户不存在！');
        }

        $data = [
            'user_id' => $user_id,
            'friend_user_id' => $friend_user_id,
            'status' => ['in', [0, 1]]
        ];

        $friInfo = self::$model->where($data)->find();
        if ($friInfo) {
            $this->error('该会员已申请或者已是好友！');
        }

        unset($data['status']);

        $data['content'] = $content;
        $data['friend_nckname'] = $friendInfo->nickname;
        $first = get_first_charter($friendInfo->nickname);
        if ($first) {
            $data['friend_first'] = $first;
        } else {
            $data['friend_first'] = '0';
        }

        $res = self::$model->create($data);
        if (!$res->id) {
            $this->error('添加申请发送失败！');
        }

        $this->success('添加申请发送成功！');
    }

    /**
     * 申请列表
     */
    public function applyLists()
    {
        $pageSize = input('pagesize', 15);

        $user_id = $this->auth->id;
        $list = self::$model->field('user_id,content')->where('friend_user_id', $user_id)->paginate($pageSize);

        $this->success('被申请列表', $list);
    }

    /**
     * 申请详情
     */
    public function read()
    {
        $id = input('id');
        if (!$id) {
            $this->error('参数缺失！');
        }
        $user_id = $this->auth->id;
        $row_info = self::$model->field('id,user_id,content,status')->where('id', $id)->where('friend_user_id', $user_id)->find();
        if (!$row_info) {
            $this->error('该申请不存在！');
        }

        $row_info->user;

        $this->success('申请详情', $row_info);
    }

    /**
     * 处理申请
     */
    public function handleApply()
    {
        $id = input('id');
        if (!$id) {
            $this->error('参数缺失！');
        }
        $user_id = $this->auth->id;
        $row_info = self::$model->field('id,user_id,content,status')->where('id', $id)->where('friend_user_id', $user_id)->find();
        if (!$row_info) {
            $this->error('该申请不存在！');
        }

        if ($row_info->status != 0) {
            $this->error('该申请已处理！');
        }

        $status = input('status', 1);
        if (!in_array($status, [-1, 1])) {
            $this->error('非法参数！');
        }

        $row_info->status = $status;
        $res = $row_info->save();

        if (!$res) {
            $this->error('处理失败！');
        }

        $this->success('处理成功！');
    }
}
